home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
programs.arc
/
SANAL.PRO
< prev
Wrap
Text File
|
1986-10-07
|
11KB
|
480 lines
/* SENTENCE ANALYSIS */
/*
This sample shows how sentence analysis
can be done in TURBO-PROLOG.
As an example, the following sentence
can be recognized:
"every man that lives loves a woman"
*/
DATABASE /* words which can be recognized */
noun( STRING )
det( STRING )
rel( STRING )
verb( STRING )
include "menu.pro"
DOMAINS
SENTENCE = sent( NOUNP, VERBP )
NOUNP = nounp( DETERM, STRING, RELCL)
DETERM = none ; determ( STRING )
RELCL = none ; relcl( STRING, VERBP )
VERBP = verb( STRING ) ; verbp( STRING, NOUNP )
TOKL = STRING*
/*
Domains for the tree with positions
*/
d_SENTENCE = sent( d_NOUNP, d_VERBP )
d_NOUNP = nounp( d_DETERM, COL, d_RELCL)
d_DETERM = none ; determ( COL )
d_RELCL = none ; relcl( COL, d_VERBP )
d_VERBP = verb( COL ) ; verbp( COL, d_NOUNP )
ATTR = INTEGER
ROW, COL = INTEGER
COLL = COL*
PREDICATES
/*
Recognition of words in different forms
*/
is_noun( STRING )
is_det( STRING )
is_rel( STRING )
is_verb( STRING )
/*
Parser
*/
s_nounp( TOKL, TOKL, COLL, COLL, NOUNP, d_NOUNP )
s_determ( TOKL, TOKL, COLL, COLL, DETERM, d_DETERM )
s_sentence( TOKL, TOKL, COLL, COLL, SENTENCE, d_SENTENCE )
s_relcl( TOKL, TOKL, COLL, COLL, RELCL, d_RELCL )
s_verbp( TOKL, TOKL, COLL, COLL, VERBP, d_VERBP )
/*
draw a sentence tree
*/
draw_sentence( ROW, ROW, d_SENTENCE, SENTENCE, COL )
draw_nounp( ROW, ROW, d_NOUNP, NOUNP, COL )
draw_relcl( ROW, ROW, d_RELCL, RELCL, COL )
draw_verbp( ROW, ROW, d_VERBP, VERBP, COL )
/*
Miscellaneous drawing predicates
*/
lin(ROW,COL,ROW,COL)
line_ver(ROW,ROW,COL)
line_hor(COL,COL,ROW)
scr_tegn(ROW,COL,CHAR)
mark(ROW,COL,STRING,ATTR)
mark2(ROW,COL,STRING,ATTR)
markfinal(ROW,COL,STRING,STRING)
mk_ulin(STRING,STRING)
writetext(ROW,COL,STRING,ATTR)
/*
scanner
*/
tokl( COL, COLL, STRING, TOKL )
check(STRING)
tom(TOKL).
/*
Main predicates
*/
sanal
run1
run2(STRING)
draw
repeat
key
process(INTEGER)
/*
Update database predicates
*/
updatdba
updatdba1(INTEGER)
read(STRING,STRING)
GOAL
makewindow(1,7,0,"",0,0,24,80),
sanal.
CLAUSES
repeat.
repeat:-repeat.
sanal:-repeat,
menu(10,20,
[ "Tutorial",
"Call dos-system",
"Call editor",
"",
"Load database from file",
"Save database on file",
"",
"Analyze a sentence",
"",
"Show/update the language"],CHOICE),
process(CHOICE),CHOICE=0,!.
process(0):-
write("\nAre you sure ? (y/n): "),
readchar(T),
T='y'.
process(1):-
file_str("sanal.hlp",TXT),
display(TXT),
clearwindow,!.
process(1):-write(">> Can't read sanal.hlp\n").
process(2):-
makewindow(3,7,0,"",0,0,25,80),
system(""),!,
removewindow.
process(2):-
write(">> command.com not accesible. press any"),
readchar(_),
removewindow.
process(3):-
edit("",_),
clearwindow.
process(4).
process(5):-consult("sanal.dba"),!.
process(5):-write(">> Can't read sanal.dba\n").
process(6):-
deletefile("sanal.bak"),
renamefile("sanal.dba","sanal.bak"),
save("sanal.dba").
process(7).
process(8):-draw.
process(9).
process(10):-updatdba.
draw:-
makewindow(1,7,0,"",0,0,25,80),
run1.
draw:-removewindow.
run1:-cursor(23,0),
write("write a sentence:\n"),
readln(LN),
run2(LN),!,
run1.
run2(LN):-
clearwindow,
tokl(5,POSL,LN,TOKL),
s_sentence( TOKL, _, POSL, _, SENT, POS ),
cursor(18,0),
write("SENTENCE=",LN),nl,nl,
write("PROLOG OBJECT=",SENT),
draw_sentence( 4, 0, POS, SENT, COL), COL<0.
run2(_).
/*
Update database
*/
updatdba:-
repeat,
menu(10,20,
[ "Show verbs",
"Show nouns",
"Show relatives",
"Show determiners",
"",
"New verbs",
"New nouns",
"New relatives",
"New determiners"],CHOICE),
updatdba1(CHOICE),
CHOICE=0,!.
updatdba1(0).
updatdba1(1):-
write("\n\nVerbs:\n******\n"),
verb(X),write(X,' '),
fail.
updatdba1(1):-nl,key.
updatdba1(2):-
write("\n\nNouns:\n******\n"),
noun(X),write(X,' '),
fail.
updatdba1(2):-nl,key.
updatdba1(3):-
write("\n\nRelatives:\n**********\n"),
rel(X),
write(X,' '),
fail.
updatdba1(3):-nl,key.
updatdba1(4):-
write("\n\nDeterminers:\n************\n"),
det(X),
write(X,' '),
fail.
updatdba1(4):-nl,key.
updatdba1(6):-
read("New verb",X),
assert(verb(X)).
updatdba1(7):-
read("New noun",X),
assert(noun(X)).
updatdba1(8):-
read("New relative",X),
assert(rel(X)).
updatdba1(9):-
read("New determiner",X),
assert(det(X)).
read(TXT,ANS):-nl,
write(TXT,": "),
readln(ANS),ANS><"".
key:-
makewindow(9,135,0,"",0,0,1,18),
write(">> Press any key"),
readkey(_),
removewindow.
tom([]).
s_sentence(TOKL,TOKL2,COLL,COLL2,sent(NOUNP,VERBP),
sent(D_NOUNP,D_VERBP)):-
s_nounp(TOKL,TOKL1,COLL,COLL1,NOUNP,D_NOUNP),
s_verbp(TOKL1,TOKL2,COLL1,COLL2,VERBP,D_VERBP),
tom(TOKL2),!.
s_sentence(_,_,_,_,_,_):-
write(">> Sentence not recognized (Use F8 to get the old line)\n"),fail.
s_nounp(TOKL,TOKL2,COLL,COLL2,nounp(DETERM,NOUN,RELCL),
nounp(D_DETERM,COL,D_RELCL)):-
s_determ(TOKL,[NOUN|TOKL1],COLL,[COL|COLL1],DETERM,D_DETERM),
is_noun(NOUN),
s_relcl(TOKL1,TOKL2,COLL1,COLL2,RELCL,D_RELCL).
s_determ([DETERM|TOKL],TOKL,[COL|COLL],COLL,determ(DETERM),
determ(COL)):-
is_det(DETERM).
s_determ(TOKL,TOKL,COLL,COLL,none,none).
s_relcl([REL|TOKL],TOKL1,[COL|COLL],COLL1,relcl(REL,VERBP),
relcl(COL,D_VERBP) ):-
is_rel(REL),
s_verbp(TOKL,TOKL1,COLL,COLL1,VERBP,D_VERBP).
s_relcl(TOKL,TOKL,COLL,COLL,none,none).
s_verbp([VERB|TOKL],TOKL1,[COL|COLL],COLL1,verbp(VERB,NOUNP),
verbp(COL,D_NOUNP)):-
is_verb(VERB),
s_nounp(TOKL,TOKL1,COLL,COLL1,NOUNP,D_NOUNP).
s_verbp([VERB|TOKL],TOKL,[COL|COLL],COLL,verb(VERB),verb(COL)):-
is_verb(VERB).
tokl(POS,[POS1|POSL],STR,[TOK|TOKL]) :-
fronttoken(STR,TOK,STR1),
check(TOK),!,
str_len(TOK,LEN),
POS1=POS+(LEN+1) div 2,
POS2=POS+5+LEN,
tokl(POS2,POSL,STR1,TOKL).
tokl(_,[],_,[]).
check(WORD):-is_noun(WORD),!.
check(WORD):-is_det(WORD),!.
check(WORD):-is_rel(WORD),!.
check(WORD):-is_verb(WORD),!.
check(WORD):-
write(">> Unknown word: ",WORD),nl,
readchar(_).
is_noun(X):-noun(X).
is_noun(X):-noun(Y),concat(Y,"s",X).
is_det(X):-det(X).
is_rel(X):-rel(X).
is_verb(X):-verb(X).
is_verb(X):-verb(Y),concat(Y,"s",X).
is_verb(X):-verb(Y),concat(Y,"ed",X).
is_verb(X):-verb(Y),concat(Y,"es",X).
is_verb(X):-verb(Y),concat(Y,"ing",X).
draw_sentence(STEP,DEPT,sent(D_NOUNP,D_VERBP),sent(NOUNP,VERBP),COL):-
DEPT1=DEPT+STEP,
draw_nounp(STEP,DEPT1,D_NOUNP,NOUNP,COL1),
draw_verbp(STEP,DEPT1,D_VERBP,VERBP,COL2),
COL=(COL1+COL2) div 2,
lin(DEPT,COL,DEPT1,COL1),
lin(DEPT,COL,DEPT1,COL2),
mark(DEPT,COL,"SENTENCE",33).
draw_nounp(STEP,DEPT,nounp(none,COL,none),nounp(_,NOUN,_),COL):-
DEPT1=DEPT+STEP div 2,
lin(DEPT1,COL,DEPT,COL),
markfinal(DEPT1,COL,"NOUN",NOUN),
mark(DEPT,COL,"NOUNP",33).
draw_nounp(STEP,DEPT,nounp(determ(COL1),COL2,none),
nounp(determ(DET),NOUN,_),COL):-
DEPT1=DEPT+STEP,
COL=(COL1+COL2) div 2,
lin(DEPT1,COL1,DEPT,COL),
lin(DEPT1,COL2,DEPT,COL),
markfinal(DEPT1,COL1,"DETERM",DET),
markfinal(DEPT1,COL2,"NOUN",NOUN),
mark(DEPT,COl,"NOUNP",33).
draw_nounp(STEP,DEPT,nounp(none,COL1,relcl(REL,VERBP)),
nounp(none,NOUN,RELCL),COL):-
DEPT1=DEPT+STEP,
draw_relcl(STEP,DEPT1,relcl(REL,VERBP),RELCL,COL2),
COL=(COL1+COL2) div 2,
lin(DEPT1,COL1,DEPT,COL),
lin(DEPT1,COL2,DEPT,COL),
markfinal(DEPT1,COL1,"NOUN",NOUN),
mark(DEPT,COL,"NOUNP",33).
draw_nounp(STEP,DEPT,nounp(determ(COL1),COL2,relcl(REL,VERBP)),
nounp(determ(DET),NOUN,RELCL),COL):-
DEPT1=DEPT+STEP,
draw_relcl(STEP,DEPT1,relcl(REL,VERBP),RELCL,COL3),
COL=(COL1+COL2+COL3) div 3,
lin(DEPT1,COL1,DEPT,COL),
lin(DEPT1,COL2,DEPT,COL),
lin(DEPT1,COL3,DEPT,COL),
markfinal(DEPT1,COL1,"DETERM",DET),
markfinal(DEPT1,COL2,"NOUN",NOUN),
mark(DEPT,COL,"NOUNP",33).
draw_verbp(STEP,DEPT,verb(COL),verb(VERB),COL):-
DEPT1=DEPT+STEP div 2,
lin(DEPT1,COL,DEPT,COL),
markfinal(DEPT1,COL,"VERB",VERB),
mark(DEPT,COL,"VERBP",33).
draw_verbp(STEP,DEPT,verbp(COL1,D_NOUNP),verbp(VERB,NOUNP),COL):-
DEPT1=DEPT+STEP,
draw_nounp(STEP,DEPT1,D_NOUNP,NOUNP,COL2),
COL=(COL1+COL2) div 2,
lin(DEPT1,COL1,DEPT,COL),
lin(DEPT1,COL2,DEPT,COL),
markfinal(DEPT1,COL1,"VERB",VERB),
mark(DEPT,COL,"VERBP",33).
draw_relcl(STEP,DEPT,relcl(COL1,D_VERBP),relcl(REL,VERBP),COL):-
DEPT1=DEPT+STEP,
draw_verbp(STEP,DEPT1,D_VERBP,VERBP,COL2),
COL=(COL1+COL2) div 2,
lin(DEPT1,COL1,DEPT,COL),
lin(DEPT1,COL2,DEPT,COL),
markfinal(DEPT1,COL1,"REL",REL),
mark(DEPT,COL,"RELCL",33).
lin(R1,C,R2,C):-!,
line_ver(R1,R2,C).
lin(R1,C1,R2,C2):-
RM=(R1+R2) div 2,
line_ver(R1,RM,C1),
line_hor(C1,C2,RM),
line_ver(RM,R2,C2),
scr_tegn(RM,C1,'+'),
scr_tegn(RM,C2,'+').
line_ver(R,R,_):-!.
line_ver(R1,R2,C):-
R2>R1,!,
scr_tegn(R1,C,'|'),
R=R1+1,
line_ver(R,R2,C).
line_ver(R2,R1,C):-
scr_tegn(R1,C,'|'),
R=R1+1,
line_ver(R,R2,C).
line_hor(C,C,_):-!.
line_hor(C1,C2,R):-
C2>C1,!,
scr_tegn(R,C1,'-'),
C=C1+1,
line_hor(C,C2,R).
line_hor(C2,C1,R):-
scr_tegn(R,C1,'-'),
C=C1+1,
line_hor(C,C2,R).
mark(ROW,COL,TEXT,ATTR):-
str_len(TEXT,LEN),
C=COL-(LEN-1) div 2,
writetext(ROW,C,TEXT,ATTR).
mark2(ROW,COL,TEXT,ATTR):-
str_len(TEXT,LEN),
C=COL-LEN div 2,
writetext(ROW,C,TEXT,ATTR).
markfinal(ROW,COL,TEXT1,TEXT2):-
str_len(TEXT1,L1),
str_len(TEXT2,L2),
L2>L1,!,
R1=ROW+1, R2=ROW+2,
mk_ulin(TEXT1,ULINE),
mark2(ROW,COL,TEXT1,33),
mark2(R1,COL,ULINE,7),
mark(R2,COL,TEXT2,112).
markfinal(ROW,COL,TEXT1,TEXT2):-
str_len(TEXT1,L),
str_len(TEXT2,L),!,
R1=ROW+1,
R2=ROW+2,
mk_ulin(TEXT1,ULINE),
mark(ROW,COL,TEXT1,33),
mark(R1,COL,ULINE,7),
mark(R2,COL,TEXT2,112).
markfinal(ROW,COL,TEXT1,TEXT2):-
R1=ROW+1,
R2=ROW+2,
mk_ulin(TEXT1,ULINE),
mark(ROW,COL,TEXT1,33),
mark(R1,COL,ULINE,7),
mark2(R2,COL,TEXT2,112).
mk_ulin(STR1,STR2):-
frontchar(STR1,_,REST),!,
mk_ulin(REST,ULI1),
concat(ULI1,"-",STR2).
mk_ulin("","").
scr_tegn(R,C,CH):-
R<25,
C<80,!,
scr_char(R,C,CH).
scr_tegn(_,_,_).
writetext(ROW,COL,TEXT,ATTR):-
ROW<25,
COL<80,
frontchar(TEXT,CH,REST),!,
scr_char(ROW,COL,CH),
scr_attr(ROW,COL,ATTR),
COL1=COL+1,
writetext(ROW,COL1,REST,ATTR).
writetext(_,_,_,_).